<template>
  <div style="background-color: #FFFFFF; padding: 20px;">
    <div>
      <div style="margin: 0 0 10px;overflow: hidden">
        <div style="float: left;">
          <router-link :to="{name: 'casesDetailEdit'}">
            <el-button type="primary" plain>添加</el-button>
          </router-link>
          <el-button type="danger" plain @click="delObjs()">删除</el-button>
          <el-button type="primary" :disabled="!is_show" plain @click="inputFunc()">查询</el-button>
          <el-switch style="padding-left: 15px" v-model="is_show">
          </el-switch>
        </div>
      </div>

      <div v-if="is_show">

        <div class="input-data">
          <el-select v-model="test_status" placeholder="测试结果">
            <el-option
              v-for="(item,index) in optionsTestStatus"
              :key="index"
              :label="item.value"
              :value="item.id">
            </el-option>
          </el-select>
        </div>

        <div class="input-data">
          <el-select v-model="status" placeholder="请选择类型">
            <el-option
              v-for="(item,index) in optionsCommon"
              :key="index"
              :label="item.value"
              :value="item.id">
            </el-option>
          </el-select>
        </div>

        <!--所属用例-->
        <div class="input-data">
          <el-select
            v-model="optionsCasesId"
            multiple
            filterable
            remote
            reserve-keyword
            placeholder="请输入用例名称"
            :remote-method="getCasesInfo"
            :loading="loading">
            <el-option
              v-for="(item,index) in cases_info"
              :key="index"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </div>

        <div class="input-data">
          <el-input placeholder="请输入步骤标题" v-model="inputObjName"></el-input>
        </div>
      </div>

      <div>
        <!--内容组件-->
        <el-table
          ref="multipleTable"
          :data="tableData"
          style="width: 100%"
          tooltip-effect="dark"
          @selection-change="handleSelectionChange">

          <el-table-column
            type="selection"
            width="55">
          </el-table-column>

          <el-table-column
            prop="id"
            label="ID"
            width="50">
          </el-table-column>

          <el-table-column
            prop="title"
            label="步骤标题"
            width="300">
            <template slot-scope="scope">
              <div class="mytable" v-text="scope.row.title">
              </div>
            </template>
          </el-table-column>

          <el-table-column
            label="所属用例"
            width="250">
            <template slot-scope="scope">
              <el-popover trigger="hover" placement="top">
                <p>用例ID: {{ scope.row.cases_info.id }}</p>
                <p>用例名称: {{ scope.row.cases_info.name }}</p>
                <div slot="reference" class="name-wrapper">
                  <el-tag size="medium">{{ scope.row.cases_info.name }} - {{ scope.row.cases_info.id }}</el-tag>
                </div>
              </el-popover>
            </template>
          </el-table-column>

          <el-table-column
            label="分发实现模块"
            width="150">
            <template slot-scope="scope">
              <el-popover trigger="hover" placement="top">
                <p>数据ID: {{ scope.row.content_object_info.id }}</p>
                <p>对应表: {{ scope.row.content_object_info.table_name }}</p>
                <div slot="reference" class="name-wrapper">
                  <el-tag size="medium">{{ scope.row.content_object_info.table_name }} - {{ scope.row.content_object_info.id }}</el-tag>
                </div>
              </el-popover>
            </template>
          </el-table-column>

          <el-table-column
            label="KEY"
            width="200">
            <template slot-scope="scope">
              <div>{{ scope.row.content_object_info.obj_key }}</div>
            </template>
          </el-table-column>

          <el-table-column
            label="分发实现标题"
            width="200">
            <template slot-scope="scope">
              <div class="mytable" v-text="scope.row.content_object_info.title">
              </div>
            </template>
          </el-table-column>

          <el-table-column
            label="步骤模板"
            width="300">
            <template slot-scope="scope">
              <div class="mytable" v-text="scope.row.steps_data_template">
              </div>
            </template>
          </el-table-column>

          <el-table-column
            label="步骤数据"
            width="300">
            <template slot-scope="scope">
              <div class="mytable" v-text="scope.row.steps_data">
              </div>
            </template>
          </el-table-column>

          <el-table-column
            label="错误信息"
            width="300">
            <template slot-scope="scope">
              <div class="mytable" v-text="scope.row.error_info">
              </div>
            </template>
          </el-table-column>

          <el-table-column prop="status" label="步骤顺序" width="120">
            <template slot-scope="scope">
              <el-select size="mini" v-model="scope.row.sort" disabled placeholder="请选择">
                <el-option
                  v-for="(item,index) in optionsSort"
                  :key="index"
                  :label="item.value"
                  :value="item.id">
                </el-option>
              </el-select>
            </template>
          </el-table-column>

          <el-table-column fixed="right" prop="results" label="执行状态" width="120">
            <template slot-scope="scope">
              <el-select size="mini" v-model="scope.row.results" disabled placeholder="请选择">
                <el-option
                  v-for="(item,index) in optionsResults"
                  :key="index"
                  :label="item.value"
                  :value="item.id">
                </el-option>
              </el-select>
            </template>
          </el-table-column>

          <el-table-column fixed="right" prop="assert_status" label="状态断言" width="120">
            <template slot-scope="scope">
              <el-select size="mini" v-model="scope.row.assert_status" disabled placeholder="请选择">
                <el-option
                  v-for="(item,index) in optionsAssertStatus"
                  :key="index"
                  :label="item.value"
                  :value="item.id">
                </el-option>
              </el-select>
            </template>
          </el-table-column>

          <el-table-column fixed="right" prop="status" label="测试结果" width="120">
            <template slot-scope="scope">
              <el-select size="mini" v-model="scope.row.test_status" disabled placeholder="请选择">
                <el-option
                  v-for="(item,index) in optionsTestStatus"
                  :key="index"
                  :label="item.value"
                  :value="item.id">
                </el-option>
              </el-select>
            </template>
          </el-table-column>

          <el-table-column
            prop="execution_time"
            label="执行时间"
            width="200">
          </el-table-column>

          <el-table-column
             fixed="right"
            prop="consume_time"
            label="步骤耗时"
            width="150">
          </el-table-column>

          <!--<el-table-column-->
            <!--fixed="right"-->
            <!--prop="exe_number"-->
            <!--label="执行次数"-->
            <!--width="100">-->
          <!--</el-table-column>-->

          <el-table-column fixed="right" prop="status" label="状态" width="120">
            <template slot-scope="scope">
              <el-select size="mini" v-model="scope.row.status" disabled placeholder="请选择类型">
                <el-option
                  v-for="(item,index) in optionsCommon"
                  :key="index"
                  :label="item.value"
                  :value="item.id">
                </el-option>
              </el-select>
            </template>
          </el-table-column>

          <el-table-column
            fixed="right"
            label="操作"
            width="130">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="selectObj(scope.row.content_object_info)">查看</el-button>
              <router-link :to="{name: 'casesDetailEdit', query: { cases_detail_id: scope.row.id }}">
                <el-button type="text" size="small">编辑</el-button>
              </router-link>
              <el-button type="text" size="small" @click="delObjs(scope.row.id)">删除</el-button>

            </template>
          </el-table-column>
        </el-table>
      </div>
      <!--分页组件-->
      <div class="block paging">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="pageSizes"
          prev-text="上一页"
          next-text="下一页"
          layout="total, sizes, prev, pager, next, jumper"
          :total="dataCount">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    name: 'CasesDetail',
    data() {
      return {
        // 搜索功能实现
        is_show: false,
        status: '',  // 状态
        test_status: '',  // 测试结果
        // 需要搜索的名称
        inputObjName: '',  // 搜索功能实现的字符串
        // choices设置
        optionsCommon: [],  // 存储CommonChoices实际数据的列表
        optionsTestStatus: [],  // 测试结果
        optionsSort: [],  // 步骤顺序
        optionsResults:[],
        optionsAssertStatus:[],

        // 分页设置
        currentPage: 1, //当前页码
        pageSize: 10,    //    每页的条数
        pageSizes: [10, 20, 30, 40],  // 可选每页显示条数
        dataCount: 0,  // 数据总条数

        cases_info: [],// 用例表，获取1000条项目数据

        // 编辑和添加选中的数据id
        loading: false,
        optionsCasesId: [],

        // 数据设置
        ListDataURL: this.$http.casesDetailURL,  // 表格列表数据获取
        objID: 'cases_detail_id',
        tableData: [],  // 阿贾克斯获取的初始数据
        multipleSelection: [],  // 不能删，我也不知道是干嘛用的，删了会报错
      }
    },
    created() {
      // 发送啊假克丝请求
      this.objList();// 阿贾克斯获取的初始数据
      this.optionsCommon = this.getChoices.common;  // 获取标准CHOICES
      this.optionsTestStatus = this.getChoices.test_status;  // 测试结果
      this.optionsSort = this.getChoices.sort;  // 步骤顺序
      this.optionsResults = this.getChoices.results;  // 执行状态
      this.optionsAssertStatus = this.getChoices.results;  // 状态断言
    },

    methods: {
      selectObj(obj){
        if (obj) {
          let table_detail = [
            ['接口明细表','ApiDetail','apiDetail_id'],
            ['WEB步骤表','Steps','steps_id'],
          ];
          table_detail.forEach(lis=>{
            if (obj.table_name === lis[0]) {
              let query = {};
              query[lis[2]] = obj.id;
              this.$router.push({path: lis[1],query:query});
            }
          })
        }
      },

      handleClick(row) {
        console.log(row);
      },
      // 修改每页显示条数
      handleSizeChange(val) {
        this.pageSize = val;  // 修改每页显示条数
        this.currentPage = 1;  // 修改页码
      },
      // 修改页码
      handleCurrentChange(val) {
        this.currentPage = val;  // 修改页码
      },

      // 发送啊假克丝请求 得到表格的原始数据
      objList() {
        this.$myTool.objList(this)
      },

      // 删除函数，调用了删除接口
      delObjs(objId) {
        this.$myTool.delObjs(objId, this)
      },

      //  按项目名称搜索的函数
      inputFunc() {
        this.$myTool.inputFunc(this)
      },

      getInputData() {
        return {
          cases_id: this.optionsCasesId,
          cases_detail_status: this.status,
          cases_detail_test_status: this.test_status,
          inputObjName: this.inputObjName
        }
      },

      getCasesInfo(query) {
        this.$myTool.getObjList(this, this.$http.casesURL, this.cases_info, query)
      },

      // 不能删
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },

    },

    watch: {

      'pageSize': function (value) {
        this.objList();
      },
      'currentPage': function (value) {
        this.objList();
      },

      optionsCasesId: function (value) {
        value = this.$myTool.delLenghtList(1, value, this);
      },
    },

    computed: {
      getChoices() {
        return this.$store.state.choicesInfo
      }
    },
  }
</script>

<style scoped>
  .paging {
    padding-top: 20px;
  }

  .input-data {
    float: left;
    width: 200px;
    padding: 5px 5px;
  }

  .mytable {
    /*width: 120px;*/
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
</style>
